﻿<body>
<p>Test for <a href="http://bugs.webkit.org/show_bug.cgi?id=3387">bug 3387</a>:
Redundant keydown, keypress, keyup events sent for arrow keys.</p>

<p>Try pressing arrow keys, PgUp/PgDown/Home/End, Esc, or function keys. 
The test passes if the box below doesn't turn red.<p>

<div id="result" style="width:100px; height:100px; background-color:blue;"></div>

<script>

    var console_messages = document.createElement("ol");
    document.body.appendChild(console_messages);
    
    window.onkeydown = registerWindow;
    window.onkeypress = registerWindow;
    window.onkeyup = registerWindow;

    document.onkeydown = registerDocument;
    document.onkeypress = registerDocument;
    document.onkeyup = registerDocument;

    document.body.onkeydown = registerBody;
    document.body.onkeypress = registerBody;
    document.body.onkeyup = registerBody;

    document.documentElement.onkeydown = registerDocumentElement;
    document.documentElement.onkeypress = registerDocumentElement;
    document.documentElement.onkeyup = registerDocumentElement;

    var bodyKeyDownCount = 0;
    var documentElementKeyDownCount = 0;
    var windowKeyDownCount = 0;
    var documentKeyDownCount = 0;

    function log(message)
    {
        var item = document.createElement("li");
        item.appendChild(document.createTextNode(message));
        item.style.fontSize = '8px';
        console_messages.appendChild(item);
    }

    function registerBody(e)
    {
        if ((e.type == "keydown" && ++bodyKeyDownCount != 1)
                || (e.type == "keyup" && --bodyKeyDownCount != 0))
            document.getElementById("result").style.backgroundColor = "red";

        if (!e)
            e = window.event;
        log("body: " + e.type);
        return true;
    }

    function registerDocumentElement(e)
    {
        if ((e.type == "keydown" && ++documentElementKeyDownCount != 1)
                || (e.type == "keyup" && --documentElementKeyDownCount != 0))
            document.getElementById("result").style.backgroundColor = "red";

        if (!e)
            e = window.event;
        log(" documentElement: " + e.type);
        return true;
    }

    function registerDocument(e)
    {
        if ((e.type == "keydown" && ++documentKeyDownCount != 1)
                || (e.type == "keyup" && --documentKeyDownCount != 0))
            document.getElementById("result").style.backgroundColor = "red";

        if (!e)
            e = window.event;
        log("  document: " + e.type);
        return true;
    }

    function registerWindow(e)
    {
        if ((e.type == "keydown" && ++windowKeyDownCount != 1)
                || (e.type == "keyup" && --windowKeyDownCount != 0))
            document.getElementById("result").style.backgroundColor = "red";

        if (!e)
            e = window.event;
        log("   window: " + e.type);
        return true;
    }

</script>
</body>
